import { request } from '../utils/request'

// 视频流API

/**
 * 开始视频流
 * @param {Object} params - 流参数
 * @param {string} params.channelId - 通道ID
 * @param {string} params.format - 流格式 (hls/mjpeg/rtmp/webrtc)
 * @param {string} params.quality - 质量 (high/medium/low)
 * @param {string} params.resolution - 分辨率
 * @param {number} params.bitrate - 比特率
 * @param {number} params.framerate - 帧率
 * @returns {Promise}
 */
export const startStream = params => {
  return request.post('/streaming/start', params, {
    loadingText: '正在启动视频流...'
  })
}

/**
 * 停止视频流
 * @param {string} sessionId - 会话ID
 * @returns {Promise}
 */
export const stopStream = sessionId => {
  return request.post(
    '/streaming/stop',
    { sessionId },
    {
      loadingText: '正在停止视频流...'
    }
  )
}

/**
 * 获取MJPEG流
 * @param {string} channelId - 通道ID
 * @param {Object} params - 流参数
 * @returns {Promise}
 */
export const getMjpegStream = (channelId, params = {}) => {
  return request.get(`/streaming/mjpeg/${channelId}`, params, {
    responseType: 'stream',
    loading: false
  })
}

/**
 * 获取HLS流URL
 * @param {string} channelId - 通道ID
 * @param {Object} params - 流参数
 * @returns {Promise}
 */
export const getHlsStreamUrl = (channelId, params = {}) => {
  return request.get(`/streaming/hls/${channelId}/playlist.m3u8`, params, {
    loading: false
  })
}

/**
 * 获取快照
 * @param {string} channelId - 通道ID
 * @param {Object} params - 快照参数
 * @param {number} params.width - 宽度
 * @param {number} params.height - 高度
 * @param {string} params.format - 格式 (jpeg/png)
 * @returns {Promise}
 */
export const getSnapshot = (channelId, params = {}) => {
  return request.get(`/streaming/snapshot/${channelId}`, params, {
    responseType: 'blob',
    loadingText: '正在获取快照...'
  })
}

/**
 * 获取流会话列表
 * @returns {Promise}
 */
export const getStreamSessions = () => {
  return request.get(
    '/streaming/sessions',
    {},
    {
      loading: false
    }
  )
}

/**
 * 获取流会话详情
 * @param {string} sessionId - 会话ID
 * @returns {Promise}
 */
export const getStreamSession = sessionId => {
  return request.get(
    `/streaming/sessions/${sessionId}`,
    {},
    {
      loading: false
    }
  )
}

/**
 * 获取流统计信息
 * @returns {Promise}
 */
export const getStreamingStatistics = () => {
  return request.get(
    '/streaming/statistics',
    {},
    {
      loading: false
    }
  )
}

/**
 * 检查流支持
 * @param {string} channelId - 通道ID
 * @returns {Promise}
 */
export const checkStreamingSupport = channelId => {
  return request.get(
    `/streaming/support/${channelId}`,
    {},
    {
      loadingText: '正在检查流支持...'
    }
  )
}

/**
 * 获取流URL
 * @param {string} channelId - 通道ID
 * @param {string} format - 格式
 * @returns {Promise}
 */
export const getStreamUrl = (channelId, format) => {
  return request.get(
    `/streaming/url/${channelId}`,
    { format },
    {
      loading: false
    }
  )
}

/**
 * 获取流配置
 * @returns {Promise}
 */
export const getStreamingConfig = () => {
  return request.get('/streaming/config')
}

/**
 * 更新流配置
 * @param {Object} config - 流配置
 * @returns {Promise}
 */
export const updateStreamingConfig = config => {
  return request.put('/streaming/config', config, {
    loadingText: '正在更新配置...'
  })
}

/**
 * 获取支持的流格式
 * @returns {Promise}
 */
export const getSupportedFormats = () => {
  return request.get(
    '/streaming/formats',
    {},
    {
      loading: false
    }
  )
}

/**
 * 获取质量配置文件
 * @returns {Promise}
 */
export const getQualityProfiles = () => {
  return request.get(
    '/streaming/quality-profiles',
    {},
    {
      loading: false
    }
  )
}

/**
 * 创建质量配置文件
 * @param {Object} profile - 质量配置文件
 * @returns {Promise}
 */
export const createQualityProfile = profile => {
  return request.post('/streaming/quality-profiles', profile, {
    loadingText: '正在创建质量配置...'
  })
}

/**
 * 更新质量配置文件
 * @param {string} profileId - 配置文件ID
 * @param {Object} profile - 质量配置文件
 * @returns {Promise}
 */
export const updateQualityProfile = (profileId, profile) => {
  return request.put(`/streaming/quality-profiles/${profileId}`, profile, {
    loadingText: '正在更新质量配置...'
  })
}

/**
 * 删除质量配置文件
 * @param {string} profileId - 配置文件ID
 * @returns {Promise}
 */
export const deleteQualityProfile = profileId => {
  return request.delete(`/streaming/quality-profiles/${profileId}`, {
    loadingText: '正在删除质量配置...'
  })
}

/**
 * 获取转码状态
 * @param {string} sessionId - 会话ID
 * @returns {Promise}
 */
export const getTranscodeStatus = sessionId => {
  return request.get(
    `/streaming/transcode/${sessionId}/status`,
    {},
    {
      loading: false
    }
  )
}

/**
 * 重启转码
 * @param {string} sessionId - 会话ID
 * @returns {Promise}
 */
export const restartTranscode = sessionId => {
  return request.post(
    `/streaming/transcode/${sessionId}/restart`,
    {},
    {
      loadingText: '正在重启转码...'
    }
  )
}

/**
 * 获取流健康状态
 * @param {string} channelId - 通道ID
 * @returns {Promise}
 */
export const getStreamHealth = channelId => {
  return request.get(
    `/streaming/health/${channelId}`,
    {},
    {
      loading: false
    }
  )
}

/**
 * 测试流连接
 * @param {string} channelId - 通道ID
 * @returns {Promise}
 */
export const testStreamConnection = channelId => {
  return request.post(
    `/streaming/test/${channelId}`,
    {},
    {
      loadingText: '正在测试流连接...'
    }
  )
}

/**
 * 获取流性能指标
 * @param {string} sessionId - 会话ID
 * @returns {Promise}
 */
export const getStreamMetrics = sessionId => {
  return request.get(
    `/streaming/metrics/${sessionId}`,
    {},
    {
      loading: false
    }
  )
}

/**
 * 获取流事件
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getStreamEvents = (params = {}) => {
  return request.get('/streaming/events', params, {
    loading: false
  })
}

/**
 * 清空流事件
 * @returns {Promise}
 */
export const clearStreamEvents = () => {
  return request.delete('/streaming/events', {
    loadingText: '正在清空事件...'
  })
}

/**
 * 获取录制列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getRecordings = (params = {}) => {
  return request.get('/streaming/recordings', params)
}

/**
 * 开始录制
 * @param {Object} params - 录制参数
 * @returns {Promise}
 */
export const startRecording = params => {
  return request.post('/streaming/recordings/start', params, {
    loadingText: '正在开始录制...'
  })
}

/**
 * 停止录制
 * @param {string} recordingId - 录制ID
 * @returns {Promise}
 */
export const stopRecording = recordingId => {
  return request.post(
    `/streaming/recordings/${recordingId}/stop`,
    {},
    {
      loadingText: '正在停止录制...'
    }
  )
}

/**
 * 下载录制文件
 * @param {string} recordingId - 录制ID
 * @returns {Promise}
 */
export const downloadRecording = recordingId => {
  return request.download(
    `/streaming/recordings/${recordingId}/download`,
    {},
    {
      loadingText: '正在下载录制文件...'
    }
  )
}

/**
 * 删除录制文件
 * @param {string} recordingId - 录制ID
 * @returns {Promise}
 */
export const deleteRecording = recordingId => {
  return request.delete(`/streaming/recordings/${recordingId}`, {
    loadingText: '正在删除录制文件...'
  })
}

/**
 * 获取播放列表
 * @param {string} channelId - 通道ID
 * @returns {Promise}
 */
export const getPlaylist = channelId => {
  return request.get(
    `/streaming/playlist/${channelId}`,
    {},
    {
      loading: false
    }
  )
}

/**
 * 创建播放列表
 * @param {Object} playlist - 播放列表
 * @returns {Promise}
 */
export const createPlaylist = playlist => {
  return request.post('/streaming/playlist', playlist, {
    loadingText: '正在创建播放列表...'
  })
}

/**
 * 更新播放列表
 * @param {string} playlistId - 播放列表ID
 * @param {Object} playlist - 播放列表
 * @returns {Promise}
 */
export const updatePlaylist = (playlistId, playlist) => {
  return request.put(`/streaming/playlist/${playlistId}`, playlist, {
    loadingText: '正在更新播放列表...'
  })
}

/**
 * 删除播放列表
 * @param {string} playlistId - 播放列表ID
 * @returns {Promise}
 */
export const deletePlaylist = playlistId => {
  return request.delete(`/streaming/playlist/${playlistId}`, {
    loadingText: '正在删除播放列表...'
  })
}

/**
 * 获取流缓存信息
 * @returns {Promise}
 */
export const getStreamCache = () => {
  return request.get(
    '/streaming/cache',
    {},
    {
      loading: false
    }
  )
}

/**
 * 清空流缓存
 * @returns {Promise}
 */
export const clearStreamCache = () => {
  return request.delete('/streaming/cache', {
    loadingText: '正在清空缓存...'
  })
}

/**
 * 获取带宽使用情况
 * @returns {Promise}
 */
export const getBandwidthUsage = () => {
  return request.get(
    '/streaming/bandwidth',
    {},
    {
      loading: false
    }
  )
}

/**
 * 设置带宽限制
 * @param {Object} limits - 带宽限制
 * @returns {Promise}
 */
export const setBandwidthLimits = limits => {
  return request.put('/streaming/bandwidth/limits', limits, {
    loadingText: '正在设置带宽限制...'
  })
}

/**
 * 获取流日志
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getStreamLogs = (params = {}) => {
  return request.get('/streaming/logs', params)
}

/**
 * 清空流日志
 * @returns {Promise}
 */
export const clearStreamLogs = () => {
  return request.delete('/streaming/logs', {
    loadingText: '正在清空日志...'
  })
}

/**
 * 导出流统计
 * @param {Object} params - 导出参数
 * @returns {Promise}
 */
export const exportStreamStatistics = (params = {}) => {
  return request.download('/streaming/statistics/export', params, {
    loadingText: '正在导出统计数据...'
  })
}
